package com.tyl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tyl.mapper.UserMapper;
import com.tyl.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;
import java.util.Map;

@SpringBootTest
public class MyBatisPlusWrapper {

    @Autowired
    private UserMapper userMapper;

    /*
    * 按用姓名字段查询
    * */
    @Test
    public void test1(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.isNotNull("name");
        userMapper.selectList(wrapper);
    }

    /*
    * 按照姓名字段通过姓名查询用户
    * */
    @Test
    public void test2(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("name","唐亚磊");
        userMapper.selectOne(wrapper);
    }

    /*
    * 按照年龄筛选用户
    * */
    @Test
    public void test3(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.between("age",17,25);
        userMapper.selectCount(wrapper);
    }

    /*
     * 模糊查询
     * */
    @Test
    public void test4(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();

        wrapper.notLike("name","t")
                .like("name","%x%");
        List<Map<String, Object>> maps = userMapper.selectMaps(wrapper);
        maps.forEach(System.out::println);
    }

    /*
     * 排序
     * */
    @Test
    public void test5(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();

        // wrapper.orderByAsc("id");
        wrapper.orderByDesc("id");
        List<Map<String, Object>> maps = userMapper.selectMaps(wrapper);
        maps.forEach(System.out::println);
    }

}
